// import { utils } from '@/utils/public';
// import { tokenStore } from '@/store/index'
import axios from 'axios'
// import { ElMessage, ElNotification } from 'element-plus'
import { message, notification } from "antd";

console.log('import.meta.env.', import.meta.env)
// axios.defaults.withCredentials = true // 允许携带cookie
axios.defaults.timeout = import.meta.env.VITE_REQUEST_TIME_OUT || 60000 // 超时时间
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8'
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
// axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.headers.put['Content-Type'] = 'application/json'



// 请求拦截器
axios.interceptors.request.use(
    config => {

        // console.log('delSub', config)
        const useTokenStore = tokenStore()
        // const data = config.data || {}
        const token = useTokenStore.token
        if (window.location.pathname !== '/login') {
            // const { token, id } = useShopInfoStore().getterShopInfo
            // console.log('sdf', data)
            // config.data = data
            if (token) {
                config.headers.Authorization = token
            }
            // if (config.header) {
            config.headers = { ...config.headers, ...config.head }
            // }
        }
        // axiosCance.addPending(config)
        return config
    },
    error => {
        return Promise.reject(error)
    }
)
// 响应拦截器
let count = 0
axios.interceptors.response.use((response) => {
    const [messageApi, contextHolder] = message.useMessage();
    if ([401].includes(Number(response.data.code)) && count === 0) {
        // ElMessage.error('请重新登录！')
        const [api, contextHolder] = notification.useNotification();
        api.info({
            message: '提示',
            description: '请重新登录！',
            duration: 1500
        })
        // ElNotification({
        //     title: '提示',
        //     message: '请重新登录！',
        //     type: 'error',
        //     duration: 1500
        // })
        sessionStorage.setItem('historyUrl', window.location.pathname + window.location.search)
        setTimeout(() => {
            // console.log('sdfsdfs进来失效')
            window.location.href = window.location.origin + '/login'
        }, 500)
        count++
    }
    return response
}, error => {
    if ([401].includes(Number(error.response.status))) {

        sessionStorage.setItem('historyUrl', window.location.pathname + window.location.search)

    } else if (Number(error.response.status) === 403) {
        messageApi.error(error.response.data.msg)
    } else if (error.response.status === 500) {
        if (error.response.data.msg.length > 50) {
            messageApi.error('服务异常！！！')
        } else {
            messageApi.error(error.response.data.msg || '操作异常')
        }
    }
    return Promise.reject(error)
})
